package com.moive.news.tqnewsapplication.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.adapter.MyProAdapter;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.entity.MyProInfo;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.GetSystemTimeUitl;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.moive.news.tqnewsapplication.weight.mylistview.SecondXListView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/11.
 * 动态中心
 */

public class ProfileSwipeActivity extends BaseActivity implements SecondXListView.IXListViewListener {
    @BindView(R.id.pub_all_listview)
    SecondXListView pubAllListview;
    MyProAdapter adapter;
    private View view;
    private Gson gson = new Gson();
    private int page = 1;
    List<MyProInfo.DataBean> list = new ArrayList<>();
    private CircleImageView img_avatar;
    private TextView user_name;
    private TextView user_friends;
    private TextView user_follows;
    private String follow = "", followed = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        ButterKnife.bind(this);
        init();
    }
    public void init() {
        view = LayoutInflater.from(ProfileSwipeActivity.this).inflate(R.layout.frofile_header, null);
        img_avatar = view.findViewById(R.id.img_avatar);
        user_name = view.findViewById(R.id.user_name);
        user_friends = view.findViewById(R.id.user_friends);
        user_follows = view.findViewById(R.id.user_follows);
        Glide.with(ProfileSwipeActivity.this)
                .load( PreferenceUtil.getString("headImg", ""))
                .error(R.drawable.icon)
                .placeholder(R.drawable.icon)
                .into(img_avatar);
        user_name.setText(PreferenceUtil.getString("nickName", ""));
        adapter = new MyProAdapter(ProfileSwipeActivity.this);
        pubAllListview.setAdapter(adapter);
        pubAllListview.setXListViewListener(this);
        pubAllListview.setPullLoadEnable(true);
        pubAllListview.addHeaderView(view);
        getdata();
        search();
    }

    @Override
    public void onRefresh() {
        page = 1;
        list.clear();
        getdata();
        adapter.notifyDataSetChanged();
        stopOnLoad();
    }

    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                page++;
                getdata();
                adapter.notifyDataSetChanged();
                stopOnLoad();
            }
        }, 1000);
    }

    private void stopOnLoad() {
        pubAllListview.stopRefresh();
        pubAllListview.stopLoadMore();
        pubAllListview.setRefreshTime(GetSystemTimeUitl.getTime());
    }

    private void getdata() {
        OkHttpUtils.post()
                .url(BaseApi.MYDYNA)
                .addParams("token",PreferenceUtil.getString("token",""))
                .addParams("userID", PreferenceUtil.getString("id",""))
                .addParams("rows", "10")
                .addParams("page", page + "")
                .build().execute(new OkManagerCallback(this) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        JSONArray jsonArray = jsonObject.getJSONArray("data");
                        if (jsonArray!=null){
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject1 = (JSONObject) jsonArray.get(i);
                                MyProInfo.DataBean dataBean = gson.fromJson(jsonObject1 + "", MyProInfo.DataBean.class);
                                list.add(dataBean);
                            }
                            adapter.addItems(list);
                        }
                        if (jsonArray.length()==0){
                            ToastUtils.ShortToast("没有更多数据了！");
                        }

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void search() {
        String userid = "";
        userid = PreferenceUtil.getString("id", "");
        OkHttpUtils.post()
                .url(BaseApi.SRARCH_FANS)
                .addParams("userID", userid)
                .build().execute(new OkManagerCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG, e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.e(TAG, response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {

                        JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                        String emotions = jsonObject1.getString("emotions");
                        String follow = jsonObject1.getString("follow");
                        String followed = jsonObject1.getString("followed");
                        user_friends.setText("关注："+follow);
                        user_follows.setText("粉丝："+followed);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
